#include <stdio.h>

#include "systemc.h"
#include "random.h"
#include "load_instructions.h"
#include "channel.h"

#define INSTRUCTION_MEMORY_LENGTH 8

SC_MODULE(imem)
{
// ------------in-out---------------------------------------------------------//
	sc_port< async_channel_in_if< sc_uint<32> > > imem_address;
	sc_port< async_channel_out_if< sc_uint<32> > > imem_data;
//----------------------------------------------------------------------------//

//-------------Variáveis-Locais-----------------------------------------------//
	sc_uint<32> addr;
//----------------------------------------------------------------------------//

	void behavior()
	{
// 		sc_uint<32> memory[] = {
// 			0x8c220000, //L: lw  r2,0(r1)
// 			0x8c640000, //   lw  r4,0(r3)
// 			0x00a42020, //   add r4,r5,r4
// 			0x00642824, //   and r5,r3,r4
// 			0xac640000, //   sw  r4,0(r3)
// 			0x00822022, //M: sub r4,r4,r2
// 			0x1080fff9, //   beq r4,r0, L
// 			0x18000005  //   j   M 
// 		};

		sc_uint<32> memory[INSTRUCTION_MEMORY_LENGTH];
		LoadInstructions(memory,INSTRUCTION_MEMORY_LENGTH,"spim.dump");

		while(true)
		{
			imem_address->read(addr);

			imem_data->write(memory[addr.range(2,0)]);
		}
	}

	SC_CTOR(imem)
	{
		SC_THREAD(behavior);
	}
};